package com.xwx.common.interceptor;
import java.util.Map;
import org.apache.log4j.Logger;
import com.jfinal.aop.Interceptor;
import com.jfinal.aop.Invocation;
import com.jfinal.kit.PropKit;
import com.xwx.common.model.OperLogModel;
import com.xwx.common.model.UserModel;

/**
* @Description:全局操作日志
* @author:Dream QQ:68697015
* @time:2015年8月17日 下午5:03:07
 */
public class OperLogInterceptor implements Interceptor{
	protected Logger log=Logger.getLogger(getClass());
	
	@Override
	public void intercept(Invocation inv) {
		OperLogModel olm = new OperLogModel();
		olm.set("module", inv.getControllerKey());
		olm.set("method",inv.getMethodName());
		UserModel um = inv.getController().getSessionAttr(PropKit.get("sessionUserKey"));
		
		if(um!=null){
			olm.set("userName", um.getStr("userName"));
		}
		//取操作参数
		StringBuffer args=new StringBuffer();
		String singleArgs=inv.getController().getPara();
		if(singleArgs!=null) {
			args.append("singleAargs:"+singleArgs);
		}
		Map<String,String[]> pars=inv.getController().getParaMap();
		for (String key : pars.keySet()) {
			args.append("##");
			args.append(key+":");
			String[] valus=pars.get(key);
			for(int i=0;i<valus.length;i++){
				args.append(valus[i]);
				if(i>valus.length-1)args.append(",");
			}
		}
		olm.set("args", args.toString());
		olm.save();
		log.info("save operLog");		
		inv.invoke();
	}

}
